<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/icons-extra.css" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">	
			<a class="mui-icon mui-icon-back mui-action-back mui-pull-left"></a>
			<h1 class="mui-title ">Geolocation</h1>
			<a class="mui-icon-extra mui-icon-extra-topic mui-pull-right" style="margin-top: 10px;"
				 onclick="openWindow('docGeolocation','../doc/geolocation.html?v=20190621')"></a>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded">
				<div class="mui-row">
					Geolocation模块管理设备位置信息，用于获取地理位置信息，如经度、纬度等。
					通过plus.geolocation可获取设备位置管理对象。
					虽然W3C已经提供标准API获取位置信息，但在某些平台存在差异或未实现，为了保持各平台的统一性，定义此规范接口获取位置信息。
				</div>
				<br />
				<div class="mui-button-row">
					<button type="button" class="mui-btn mui-btn-success mui-btn-block" onclick="watchPos()">监听设备位置</button>
					<button type="button" class="mui-btn mui-btn-primary mui-btn-block" onclick="clearWatch()">停止监听</button>
					<button type="button" class="mui-btn mui-btn-warning mui-btn-block" onclick="getGeocode()">获取定位信息</button>
				</div>
				<br />
				<div class="mui-row" id="divInfo" style="word-wrap:break-word">
					
				</div>
			</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/app.js" ></script>
		<script type="text/javascript">
			mui.init();
			function outSet(message){
				document.getElementById("divInfo").innerText = message;
			}
			
			function outLine(message){
				var text = document.getElementById("divInfo").innerText;
				document.getElementById("divInfo").innerText = text + "\n" + message;
			}
			function outInfo( position ) {
				var str = "";
				str += "地址："+position.addresses+"\n";//获取地址信息
				str += "坐标类型："+position.coordsType+"\n";
				var timeflag = position.timestamp;//获取到地理位置信息的时间戳；一个毫秒数；
				str += "时间戳："+timeflag+"\n";
				var codns = position.coords;//获取地理坐标信息；
				var lat = codns.latitude;//获取到当前位置的纬度；
				str += "纬度："+lat+"\n";
				var longt = codns.longitude;//获取到当前位置的经度
				str += "经度："+longt+"\n";
				var alt = codns.altitude;//获取到当前位置的海拔信息；
				str += "海拔："+alt+"\n";
				var accu = codns.accuracy;//地理坐标信息精确度信息；
				str += "精确度："+accu+"\n";
				var altAcc = codns.altitudeAccuracy;//获取海拔信息的精确度；
				str += "海拔精确度："+altAcc+"\n";
				var head = codns.heading;//获取设备的移动方向；
				str += "移动方向："+head+"\n";
				var sped = codns.speed;//获取设备的移动速度；
				str += "移动速度："+sped;
				console.log(JSON.stringify(position));
				outLine( str );
			}
			mui.plusReady(function(){
				outSet("获取当前设备位置信息...");
				plus.geolocation.getCurrentPosition(function(p){
					outInfo(p);
				}, function(e){
					outSet('Geolocation error: ' + e.message);
				}, {geocode:false} );
			});
			var watchId;
			function watchPos() {
				if ( watchId ) {
					return;
				}
				watchId = plus.geolocation.watchPosition( function ( p ) {
					outSet( "监听位置变化信息:" );
					outInfo( p );
				}, function ( e ) {
					outSet( "监听位置变化信息失败："+e.message );
				}, {geocode:false} );
			}
			function clearWatch() {
				if ( watchId ) {
					plus.geolocation.clearWatch( watchId );
					watchId = null;
					outLine( "停止监听位置变化信息" );
				}
			}
			
			// 通过定位模块获取位置信息
			function getGeocode(){
				outSet( "获取定位位置信息:" );
				plus.geolocation.getCurrentPosition( function(p){
					outInfo(p);
				}, function ( e ) {
					outSet( "获取定位位置信息失败："+e.message );
				},{geocode:true});
			}
		</script>
	</body>

</html>